package com.example.booksshop.service.imp;
import com.example.booksshop.entity.User;
import com.example.booksshop.mapper.UserMapper;
import com.example.booksshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xuan 2021/9/15
 */
@Service
public class UserServiceImpl implements UserService {

   @Autowired
   UserMapper userMapper;

   @Override
   public void insert(User user) {
      userMapper.insert(user);
   }

   @Override
   public User select(int id) {
      return  userMapper.select(id);
   }

   @Override
   public void delete(Integer id) {
      userMapper.delete(id);
   }
   @Override
   public int update(User user) {
      return userMapper.update(user);
   }
   @Override
   public List<User>  show() {
      return userMapper.show();
   }
   @Override
   public String userExsit(String username, String password) {
      List<User> list = new ArrayList<>();
      User u1 = new User("xuan", "123");
      list.add(u1);
      User loginUser = getUserByUsername(username, list);
      if (loginUser == null) {
         return "该用户不存在";
      }
      if (loginUser.password.equals(password)) {
         return "登录成功";
      }
      return "用户名或密码错误（密码错误）";
   }
   public User getUserByUsername(String username, List<User> list) {
      if (username == null || list == null || username.isEmpty()) {
         return null;
      }
      if (!(username instanceof String) || !(list instanceof List)){
         return null;
      }
      for (int i = 0; i < list.size(); i++) {
         User user = list.get(i);
         if (username.equals(user.getUsername())) {
            return user;
         }
      }
      return null;
   }
}

